/*
 *
 * Copyright (c) 2001-2018 泛微软件.
 * 泛微协同商务系统,版权所有.
 * 
 */
package com.engine.demo.cmd;

import com.cloudstore.eccom.constant.WeaBoolAttr;
import com.cloudstore.eccom.pc.table.*;
import com.cloudstore.eccom.result.WeaResultMsg;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.demo.constant.DemoConstant;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.conn.constant.DBConstant;
import weaver.general.PageIdConst;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.*;

/**
 * @version 1.0
 * @author AndyZhang
 * TODO 
 */
public class DoQueryDemoCmd extends AbstractCommonCommand<Map<String,Object>> {

	private final static Log log = LogFactory.getLog(DoQueryDemoCmd.class);
	
    private BizLogContext bizLogContext = new BizLogContext();

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    public DoQueryDemoCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
    	WeaResultMsg result=new WeaResultMsg(true);
        try {
            try {
                String pageUid =  DemoConstant.CONFIG_PAGEUID;
                String pageID = pageUid + "_" + user.getUID();
                String pageSize = PageIdConst.getPageSize(pageID, user.getUID());
                String sqlwhere = " 1=1 ";

                WeaTable table = new WeaTable();
                table.setPageUID(pageUid);
                table.setPageID(pageID);
                table.setPagesize(pageSize);
                RecordSet rs=new RecordSet();
                String fields = " ID, t.status as tmpstatus, EM_NAME, EM_URL, STATUS, concat(concat(T .CREATEDATE, ' '),T .CREATETIME) AS tcretime, concat(concat(T .MODIFYDATE, ' '),T .MODIFYTIME) AS tmodtime";
                if(DBConstant.DB_TYPE_SQLSERVER.equalsIgnoreCase(rs.getDBType())){
                    fields = " ID, t.status as tmpstatus, EM_NAME, EM_URL, STATUS, T .CREATEDATE + ' ' + T .CREATETIME AS tcretime, T .MODIFYDATE + ' ' + T .MODIFYTIME AS tmodtime";
                }

                table.setBackfields(fields);

                //搜索
                String emName = Util.null2String(params.get("em_name"));
                if (StringUtils.isNotEmpty(emName) && StringUtils.isNotBlank(emName)) {
                    sqlwhere += " and EM_NAME like '%" + emName + "%' ";
                }
                String emCorpId = Util.null2String(params.get("corp_id"));
                if (StringUtils.isNotEmpty(emCorpId) && StringUtils.isNotBlank(emCorpId)) {
                    sqlwhere += " and corp_id='" + emCorpId + "' ";
                }
                String emStatus = Util.null2String(params.get("status"));
                if (StringUtils.isNotEmpty(emStatus) && StringUtils.isNotBlank(emStatus)) {
                    if (StringUtils.equals(emStatus, "enabled")) {
                        sqlwhere += " and status='y' ";
                    } else if (StringUtils.equals(emStatus, "disabled")) {
                        sqlwhere += " and status='n' ";
                    }
                }
                table.setSqlform("ECOLOGY_DEMO t");
                table.setSqlwhere(sqlwhere);
                table.setSqlorderby("tmodtime,id");
                table.setSqlprimarykey("ID");
                table.setSqlisdistinct("false");
                table.setSqlsortway("DESC");
                String languageId = "" + user.getLanguage();
                table.getColumns().add(new WeaTableColumn("id").setDisplay(WeaBoolAttr.FALSE));
                table.getColumns().add(new WeaTableColumn("30%", SystemEnv.getHtmlLabelName(388658, user.getLanguage()), "em_name","em_name"));
                table.getColumns().add(new WeaTableColumn("30%", SystemEnv.getHtmlLabelName(388659, user.getLanguage()), "em_url","em_url"));
                table.getColumns().add(new WeaTableColumn("15%", SystemEnv.getHtmlLabelName(1339, user.getLanguage()), "tcretime","tcretime"));
                table.getColumns().add(new WeaTableColumn("15%", SystemEnv.getHtmlLabelName(19520, user.getLanguage()), "tmodtime","tmodtime"));
                table.getColumns().add(new WeaTableColumn("10%", SystemEnv.getHtmlLabelName(602, user.getLanguage()), "status", "status", "com.engine.systeminfo.util.DataUtils.getStatus", languageId));
                WeaTableColumn weaTableColumn = new WeaTableColumn("5%",SystemEnv.getHtmlLabelName(602,user.getLanguage()),"tmpstatus","tmpstatus","com.engine.systeminfo.util.DataUtils.getOpratePopedomWithStatus");
                weaTableColumn.setDisplay(WeaBoolAttr.FALSE);
                weaTableColumn.setTransMethodForce("true");
                table.getColumns().add(weaTableColumn);

                WeaTableOperates weaTableOperates = new WeaTableOperates();
                List<WeaTableOperate> operateList = new ArrayList<>();
                WeaTableOperate edit = new WeaTableOperate(SystemEnv.getHtmlLabelName(93, user.getLanguage()), "", "0");
                edit.setIsalwaysshow("true");

                WeaTableOperate enabled = new WeaTableOperate(SystemEnv.getHtmlLabelName(18095, user.getLanguage()), "", "1");

                WeaTableOperate disabled = new WeaTableOperate(SystemEnv.getHtmlLabelName(18096, user.getLanguage()), "", "2");

                WeaTableOperate delete = new WeaTableOperate(SystemEnv.getHtmlLabelName(91, user.getLanguage()), "", "3");
                delete.setIsalwaysshow("true");

                WeaTableOperate log = new WeaTableOperate(SystemEnv.getHtmlLabelName(83, user.getLanguage()), "", "4");
                log.setIsalwaysshow("true");

                operateList.add(edit);
                operateList.add(enabled);
                operateList.add(disabled);
                operateList.add(delete);
                operateList.add(log);

                WeaTablePopedom popedom = new WeaTablePopedom();
                popedom.setTransmethod("com.engine.systeminfo.util.DataUtils.getOpratePopedomWithStatus");
                popedom.setOtherpara("column:tmpstatus");
                weaTableOperates.setPopedom(popedom);
                weaTableOperates.setOperate(operateList);
                table.setOperates(weaTableOperates);

                result.putAll(table.makeDataResult());
                result.success();
                

            } catch (Exception e) {
                e.printStackTrace();
                log.error(e);
                return result.fail(e.getMessage()).getResultMapAll();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.getResultMapAll();
    }

}
